<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item>
          <el-col :span="4">
            <treeselect
              v-model="siteId"
              :options="websiteOptions"
              :normalizer="normalizer"
              :show-count="true"
              max-width="200"
              placeholder="所属站点"
          /></el-col>
          <el-col :span="20">
            <el-button
              v-if="permissions.admin_classlist_get"
              icon="el-icon-search"
              type="primary"
              @click="getList"
            >
              刷新
            </el-button>
            <el-button
              v-if="permissions.admin_classlist_get"
              icon="el-icon-plus"
              type="primary"
              @click="addOrUpdateHandle(false)"
            >
              添加
            </el-button>
          </el-col>
        </el-form-item>
      </el-form>

      <el-table
        border
        v-loading="loading"
        :data="classList"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildrenx' }"
      >
        <el-table-column type="index" label="编号" width="60"></el-table-column>
        <el-table-column
          prop="name"
          label="标题名称"
          :show-overflow-tooltip="true"
          width="200"
        ></el-table-column>
        <el-table-column
          label="内容数量"
          :show-overflow-tooltip="true"
          width="200"
          :formatter="getNewsCount"
        >
        </el-table-column>
        <el-table-column
          prop="type"
          label="栏目类型"
          :show-overflow-tooltip="true"
          width="200"
          :formatter="getTypeName"
        ></el-table-column>
        <el-table-column
          prop="bodykey"
          label="定位"
          :show-overflow-tooltip="true"
          width="200"
        ></el-table-column>
        <el-table-column label="导航" :show-overflow-tooltip="true" width="180">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.isMenu === 'PD10001'"
              >是</el-tag
            >
            <el-tag type="warning" v-if="scope.row.isMenu === 'PD10002'"
              >否</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="sequence"
          label="排序"
          width="60"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="80" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 'ZT10001'"
              >显示</el-tag
            >
            <el-tag type="warning" v-if="scope.row.status === 'ZT10002'"
              >隐藏</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="内容管理"
          :show-overflow-tooltip="true"
          width="200"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="toNews(scope.row)"
              >内容管理
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false, scope.row.id)"
              v-if="permissions.sdAdmin_param_get"
              >添加
            </el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true, scope.row.id)"
              v-if="permissions.sdAdmin_param_edit"
              >修改
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="permissions.sdAdmin_param_del"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getList"
        v-bind:typeDictList="typeDict"
      ></table-form>
    </div>
  </basic-container>
</template>

<script>
import { fetchClasslistTree, delObj } from "@/api/cms/classlist";
import { fetchWebsiteTree } from "@/api/sdAdmin/syswebsite";
import { remote } from "@/api/admin/dict";
import { getCountGroupByClass } from "@/api/cms/news";
import TableForm from "./classlist-form";
import { mapGetters } from "vuex";
import Treeselect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "classlist",
  components: { TableForm, Treeselect },
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: false,
      // 菜单表格树数据
      classList: [],
      typeDict: [],
      newsCountList: [],
      websiteOptions: [],
      siteId: this.$store.getters.siteId,
    };
  },
  created() {
    this.getTypeDict("LM10000");
    this.getNewsCountList(this.siteId);
    this.getTreeselect();
  },
  mounted() {
    this.getList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    /** 查询站点下拉树结构 */
    getTreeselect() {
      fetchWebsiteTree(false).then((response) => {
        this.websiteOptions = response.data.data;
      });
    },
    /** 转换参数数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id, this.classList);
      });
    },
    getNewsCountList(siteId) {
      getCountGroupByClass(siteId).then((response) => {
        this.newsCountList = response.data.data;
      });
    },
    getTypeDict(type) {
      remote(type).then((response) => {
        this.typeDict = response.data.data;
      });
    },
    getList() {
      this.$store.commit("SET_SITE_ID", this.siteId);
      this.loading = true;
      fetchClasslistTree(false).then((response) => {
        this.classList = response.data.data;
        this.loading = false;
      });
      this.getNewsCountList(this.siteId)
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.id);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        });
    },
    toNews(row) {
      this.$router.push({
        path: "/cms/news/index",
        query: { classlistId: row.id },
      });
    },
    getNewsCount(row, column) {
      if (this.newsCountList && this.newsCountList.length > 0) {
        let map = this.newsCountList.find(
          (value) => value.classlist_id == row.id
        );
        return map ? map.count : 0;
      } else {
        this.getNewsCountList(this.siteId);
        let map = this.newsCountList.find(
          (value) => value.classlist_id == row.id
        );
        return map ? map.count : 0;
      }
    },
    getTypeName(row, column) {
      if (this.typeDict && this.typeDict.length > 0) {
        return this.typeDict.find(
          (value, index, array) => value.value == row.type
        ).label;
      } else {
        this.getTypeDict("LM10000");
        return this.typeDict.find(
          (value, index, array) => value.value == row.type
        ).label;
      }
    },
  },
};
</script>
